﻿using System.Net;

namespace Model.Configuration_Model
{
    /// <summary>
    /// mqtt model
    /// </summary>
    public class MqttSettingsModel
    {
        /// <summary>
        /// MQTT username
        /// </summary>
        public string Username { get; set; }

        /// <summary>
        /// MQTT password
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        ///设置默认连接超时（秒）
        /// </summary>
        public int CommunicationTimeout { get; set; } = 5;

        /// <summary>
        ///将0设置为禁用连接日志记录
        /// </summary>
        public int ConnectionBacklog { get; set; }

        /// <summary>
        /// 启用对持久会话的支持
        /// </summary>
        public bool EnablePersistentSessions { get; set; } = false;

        /// <summary>
        /// 侦听设置
        /// </summary>
        public TcpEndPointModel TcpEndPoint { get; set; } = new TcpEndPointModel();


        /// <summary>
        /// 为每个客户端的最大挂起消息设置限制
        /// </summary>
        public int MaxPendingMessagesPerClient { get; set; } = 250;

        /// <summary>
        /// 保留信息的设置。
        /// </summary>
        public RetainedApplicationMessagesModel RetainedApplicationMessages { get; set; } = new RetainedApplicationMessagesModel();

        /// <summary>
        /// 启用或禁用MQTTnet内部日志记录。
        /// </summary>
        public bool EnableDebugLogging { get; set; } = true;

        /// <summary>
        /// Adds sender client id to the payload
        /// </summary>
        public bool UseOriginalReseiverClientId { get; set; } = true;
    }
    /// <summary>
    /// 保留信息的设置。
    /// </summary>
    public class RetainedApplicationMessagesModel
    {
        public bool Persist { get; set; } = false;

        public int WriteInterval { get; set; } = 10;

        public string Path { get; set; }
    }
    /// <summary>
    /// tcp 端点
    /// </summary>
    public class TcpEndPointModel
    {
        /// <summary>
        /// 启用/禁用
        /// </summary>
        public bool Enabled { get; set; } = true;
        /// <summary>
        /// 侦听地址
        /// </summary>
        public string IPv4 { get; set; }
        /// <summary>
        /// 侦听地址
        /// </summary>
        public string IPv6 { get; set; }

        /// <summary>
        /// 侦听端口
        /// </summary>
        public int Port { get; set; } = 1883;

        /// <summary>
        /// Read IPv4
        /// </summary>
        /// <returns></returns>
        public bool TryReadIPv4(out IPAddress address)
        {
            if (IPv4 == "*")
            {
                address = IPAddress.Any;
                return true;
            }

            if (IPv4 == "localhost")
            {
                address = IPAddress.Loopback;
                return true;
            }

            if (IPv4 == "disable")
            {
                address = IPAddress.None;
                return true;
            }

            if (IPAddress.TryParse(IPv4, out var ip))
            {
                address = ip;
                return true;
            }

            throw new System.Exception($"Could not parse IPv4 address: {IPv4}");
        }

        /// <summary>
        /// Read IPv6
        /// </summary>
        /// <returns></returns>
        public bool TryReadIPv6(out IPAddress address)
        {
            if (IPv6 == "*")
            {
                address = IPAddress.IPv6Any;
                return true;
            }

            if (IPv6 == "localhost")
            {
                address = IPAddress.IPv6Loopback;
                return true;
            }

            if (IPv6 == "disable")
            {
                address = IPAddress.None;
                return true;
            }

            if (IPAddress.TryParse(IPv6, out var ip))
            {
                address = ip;
                return true;
            }

            throw new System.Exception($"Could not parse IPv6 address: {IPv6}");
        }
    }
}
